﻿#ifndef DATAMEASUREMENT_H
#define DATAMEASUREMENT_H

#include <QDialog>
#include <QPainter>
#include "Sqlite.h"
#include "./qtOpenCV/convert.h"
#include <gsl/gsl_statistics.h>
#include <QtCore/QSemaphore>
#include "FocusMeasure.h"
#include "CameraCommon/CameraImage_RGB24.h"
#include "dialog_manualfocus.h"


const int CpkLength = 30;
const int wiatTime = 10;

class B3MainWindow;
class StageControl;

namespace Ui {
class DataMeasurement;
}

class DataMeasurementDialog : public QDialog
{
    Q_OBJECT

public:
    enum MeasuremetTYPE{ LEFT = 0, RIGHT = 1, TOP = 2, BOTTOM = 3 };
    explicit DataMeasurementDialog(QWidget *parent = 0, B3MainWindow *win = 0, CameraImage * cameraImage = 0);
    ~DataMeasurementDialog();
    void setMainWindow(B3MainWindow * win);
    void setStageControl(StageControl * stageControl);
    void setData(QString dataBaseName, QString sqlDirPath);
    void readSpecData();
    void setUiControlValue(int index);
    void addChart2TabWidget(int showDataNumber = 30);
    bool eventFilter(QObject *object, QEvent *event);   //从事件过滤器中拦截绘图事件，用于在控件中绘图
    void paintLine();   //在控件中绘图
public slots:
    void moveToPos_achieve();
    void NewPanel();
    void autoPanelMeasure();
private slots:
    void StartMeasurement_CF();
    void StartMeasurement_TFT();
    void Save();
private:
    void move2start();
    void move2end();
    double lineMeasure(QImage &img);
    void measure();
    void showData(double length);
    bool autoFocus();
    bool autoFocus2();
    bool manualFocus(double & result);

private:
    enum STATE
    {
        STATE_IDLE,
        STATE_MOVE2START_CF,
        STATE_MOVE2END_CF,
        STATE_MOVE2START_TFT,
        STATE_MOVE2END_TFT
    };
    enum STATE m_state;

    Ui::DataMeasurement *ui;
    B3MainWindow *m_win;
    StageControl *m_stageControl;
    CameraImage * m_cameraImage;

    Sqlite * m_sqlite;
    QList<MeasureSpecInfo> measureSpecInfoList;
    QList<MeasureSpecInfo> measureSpecInfoList_CF;
    QList<MeasureSpecInfo> measureSpecInfoList_TFT;

    QString m_dataBaseName;
    QString m_sqlDirPath;

    int m_index;
    int m_index_CF;
    int m_index_TFT;
    double m_currentStartLine;
    double m_currentEndLine;

    double m_currentX;
    double m_currentY;
    double m_currentZ;
    double m_currentScale;
    int m_currentLens;
    int m_currentIntensity_top;
    int m_currentIntensity_bottom;
    int m_currentExpoureTime;

    QStringList m_measureData;
    QStringList m_measureData_CF;
    QStringList m_measureData_TFT;

    double CpkData[CpkLength];

    QSemaphore m_autoFocusSemaphore;

    bool isAutoFocus;
    FocusMeasure m_focusMeasure;
    double best_score ;
    double z_best;
    QList<double> zList;
    QImage m_bestImage;

    QRect roi;

    MeasuremetTYPE m_measurementTypt;

};

#endif // DATAMEASUREMENT_H
